package com.Ajava1704.for并发包下的类.ForSemphor;

import java.util.concurrent.Semaphore;

/**
 * @类名: com.Ajava1704.for并发包下的类.ForSemphor
 * @作者: lgt
 * @创建时间: 2020-02-11 20:55
 * @描述:
 **/
public class ForSemphRunable implements Runnable {
    private Semaphore semaphore;

    private int time;

    public ForSemphRunable(Semaphore semaphore, int time) {
        this.semaphore = semaphore;
        this.time = time;
    }

    @Override
    public void run() {
        try {
            //获取资源，获取到资源的话运行以下的代码，获取不到该资源的阻塞到该步骤，直到获取到资源
            semaphore.acquire();

            //获取到资源运行该处的代码
            Thread.sleep(time * 1000);
            System.out.println(Thread.currentThread().getName() + "==========进行工作====");
            //使用完毕之后释放资源
            semaphore.release();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
